package sparrowGui
{
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	
	import sparrowGui.utils.TextFieldEx;
	
	/**
	 * html文本的提示窗
	 * 鼠标经过后一段时间弹出提示的文字,整个游戏只有一个鼠标经过提示框,因此此类为单例
	 * 使用前先在初始时将把此显示容器的父容器存入(一般是根容器),使用时直接在根容器里面初始出来
	 * @author Pelephone
	 */
	public class HtmlTextTip extends BaseTip
	{
		private var _txt:TextFieldEx;
		private var _clickTime:int;
		
		public function HtmlTextTip(parent:DisplayObjectContainer, vars:Object=null)
		{
			super(parent, vars);
		}
		
		/**
		 * 初始放入的容器
		 * @param parent
		 */		
		override protected function init():void
		{
			_txt = new TextFieldEx();
			addChild(_txt);
			this.visible = false;
			this.mouseChildren = this.mouseEnabled = false;
			_txt.textField.textColor = 0xFFFFFF;
			if(!style.width){
				_txt.textField.autoSize = TextFieldAutoSize.LEFT;
				_txt.textField.wordWrap = false;
			}
		}
		
		override protected function draw():void
		{
			super.draw();
			
			_txt.x = style.paddingLeft;
			_txt.y = style.paddingTop;
			
			if(style.width) _txt.width = style.width - style.paddingLeft - style.paddingRight;
			if(style.height) _txt.height = style.height - style.paddingTop - style.paddingBottom;
		}
		
		override public function update(data:Object):void
		{
			if(data && data is String)
				htmlText = String(data);
			super.update(data);
		}
		
		override protected function onEvt(e:Event):void
		{
			switch(e.type){
				case MouseEvent.ROLL_OVER:
					_overObj = e.currentTarget as InteractiveObject;
//					htmlText = overTip.Itip;
					update(getTipObj(overTip));
					setTipPosi(overTip);
					_clickTime = getTimer();
					addEvt()
					break;
				case Event.MOUSE_LEAVE:
				case MouseEvent.MOUSE_UP:
				case MouseEvent.ROLL_OUT:
					hiddenOverTip();
					break;
				case Event.ENTER_FRAME:
					if((getTimer()-_clickTime)>style.delay){
						if(!this.parent){
							_parent.addChild(this);
							this.visible = true;
							removeEventListener(Event.ENTER_FRAME,onEvt);
						}
						// 把这个打开再屏掉removeEventListener(Event.ENTER_FRAME,onEvt);便鼠标跟随的动
//						setTipPosi(overTip);
					}
					break;
			}
		}
		
		override public function addTipBtn(tipObj:InteractiveObject, tipData:Object):void
		{
			killTip(tipObj);	//为了防止重复给一个按钮添加了两次事件,添加新事件前将其移除
			tipObj.addEventListener(MouseEvent.ROLL_OVER,onEvt);
			tipObj.addEventListener(MouseEvent.ROLL_OUT,onEvt);
			
			super.addTipBtn(tipObj,tipData);
		}
		
		override public function killTip(tipObj:InteractiveObject):void
		{
			if(!tipObj) return;
			tipObj.removeEventListener(MouseEvent.ROLL_OVER,onEvt);
			tipObj.removeEventListener(MouseEvent.ROLL_OUT,onEvt);
			super.killTip(tipObj);
		}
		
		override protected function addEvt():void
		{
			super.addEvt();
			addEventListener(Event.ENTER_FRAME,onEvt);
		}
		
		override protected function removeEvt():void
		{
			removeEventListener(Event.ENTER_FRAME,onEvt);
			super.removeEvt();
		}
		
		/**
		 * 清除不在舞台上的所有对象的overtip
		 */		
		override public function removeNoStageEvt():void
		{
			super.removeNoStageEvt();
			removeEventListener(Event.ENTER_FRAME,onEvt);
		}
		
		private function hiddenOverTip():void
		{
			_overObj = null;
			if(this.parent) parent.removeChild(this);
			this.visible = false;
			removeEvt();
		}
		
		/**
		 * 获取文本框
		 * @return 
		 */		
		public function get tf():TextFieldEx
		{
			return _txt;
		}
		
		/**
		 * 作为文本字段中当前文本的字符串。各行之间用回车符（'\r'，即 ASCII 13）分隔。此属性包含文本字段中的无格式文本，不带 HTML 标签。 
		 * @param value
		 */		
		public function set text(value:String):void
		{
			_txt.textField.text = value;
			style.width = _txt.width + style.paddingLeft + style.paddingRight;
			style.height = _txt.height + style.paddingBottom + style.paddingTop;
			nextDraw();
		}
		
		/**
		 * 包含文本字段内容的 HTML 表示形式。 
		 * @param value
		 */		
		public function set htmlText(value:String):void
		{
			_txt.htmlText = value;
			style.width = _txt.width + style.paddingLeft + style.paddingRight;
			style.height = _txt.height + style.paddingBottom + style.paddingTop;
			nextDraw();
		}
		
		override public function set width(w:Number):void
		{
			if(w>0){
				_txt.textField.autoSize = TextFieldAutoSize.LEFT;
				_txt.textField.wordWrap = true;
				_txt.width = w - style.padding*2;
			}
			super.width = w;
		}
	}
}